package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IAnalysisDao;
import com.manager.plat.dao.IRouterDao;
import com.manager.plat.util.PageInfo;
import com.manager.plat.vo.AnalysisVo;
import com.manager.plat.vo.GatewayVo;
import com.manager.plat.vo.TermVo;
import com.manager.plat.vo.UserBillVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class AnalysisPartnerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -745339932876051245L;

	private static Logger logger = Logger.getLogger(AnalysisPartnerAction.class);
	
	private IAnalysisDao analysisDao;
	private IRouterDao routerDao;						//路由信息数据接口
	private List<AnalysisVo> analysisVoList;			//数据分析信息VO
	private List<GatewayVo> gatewayVoList;				//长号码信息列表
	private List<UserBillVo> userBillVoList;			//账单信息列表
	private TermVo term;								//查询条件VO
	
	private PageInfo pageInfo;
	private String pageIndexs;
	
	/**进入省份统计结页面*/
	public String getProvince(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		/**获得业务长号码信息列表*/
		gatewayVoList = routerDao.getLowerGatewayByUserId(userInfoVo.getId());
		term = null;
		analysisVoList = null;
		
		return SUCCESS;
	}
	/**查询省份统计信息*/
	public String getProvinceAnalysis(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得用户ID*/
		int userId = userInfoVo.getId();
		/**获得网关ID*/
		int gateway_id = term.getGateway_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		logger.info(String.format("渠道查询收入条件[gateway_id=%d, time_begin=%s, time_end=%s]", gateway_id, time_begin, time_end));
		
		analysisVoList = analysisDao.getProvinceAnalysisPartner(gateway_id, userId, time_begin, time_end);
		
		return SUCCESS;
	}
	
	
	/**进入详情页面*/
	public String getDetail(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		/**获得业务长号码信息列表*/
		gatewayVoList = routerDao.getLowerGatewayByUserId(userInfoVo.getId());
		term = null;
		userBillVoList = null;
		
		pageInfo = null;
		pageIndexs = "";
		return SUCCESS;
	}
	/**查询详情信息*/
	public String getDetailAnalysis(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		/**获得用户ID*/
		int userId = platUser.getId();
		/**获得主叫号码*/
		String mobile = term.getMobile();
		/**获得网关ID*/
		int gateway_id = term.getGateway_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		
		int total = analysisDao.getDetailAnalysisPartnerTotal(mobile, gateway_id, userId, time_begin, time_end);
		if(total == 0){
			userBillVoList = null;
			return SUCCESS;
		}
		
		pageInfo = new PageInfo();
		pageInfo.search(total);
		int currPage = 0;
		if (pageIndexs != null && !"".equals(pageIndexs)) {
			currPage = Integer.parseInt(pageIndexs);
		}
		if (currPage != 0) {
			pageInfo.turnToPage(currPage);
		}
		
		userBillVoList = analysisDao.getDetailAnalysisPartner(mobile, gateway_id, userId, time_begin, time_end, pageInfo);
		
		return SUCCESS;
	}
	
	public IAnalysisDao getAnalysisDao() {
		return analysisDao;
	}
	public void setAnalysisDao(IAnalysisDao analysisDao) {
		this.analysisDao = analysisDao;
	}
	public IRouterDao getRouterDao() {
		return routerDao;
	}
	public void setRouterDao(IRouterDao routerDao) {
		this.routerDao = routerDao;
	}
	public List<AnalysisVo> getAnalysisVoList() {
		return analysisVoList;
	}
	public void setAnalysisVoList(List<AnalysisVo> analysisVoList) {
		this.analysisVoList = analysisVoList;
	}
	public List<GatewayVo> getGatewayVoList() {
		return gatewayVoList;
	}
	public void setGatewayVoList(List<GatewayVo> gatewayVoList) {
		this.gatewayVoList = gatewayVoList;
	}
	public List<UserBillVo> getUserBillVoList() {
		return userBillVoList;
	}
	public void setUserBillVoList(List<UserBillVo> userBillVoList) {
		this.userBillVoList = userBillVoList;
	}
	public TermVo getTerm() {
		return term;
	}
	public void setTerm(TermVo term) {
		this.term = term;
	}
	public PageInfo getPageInfo() {
		return pageInfo;
	}
	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}
	public String getPageIndexs() {
		return pageIndexs;
	}
	public void setPageIndexs(String pageIndexs) {
		this.pageIndexs = pageIndexs;
	}
}
